//
//  WithdrawSecondVC.m
//  GasBusiness
//
//  Created by zfc on 2021/8/1.
//

#import "WithdrawSecondVC.h"
#import "WithdrawSecondTopCell.h"
#import "WithdrawSecondMoneyCell.h"
#import "WithdrawSecondSendMsgCell.h"
#import "WithdrawAccountVC.h"
@interface WithdrawSecondVC ()
@property (nonatomic, copy) NSArray *accountList;
@property (nonatomic, strong) UITextField *moneyTf;
@property (nonatomic, strong) UITextField *codeTf;

@end

@implementation WithdrawSecondVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"提现";
    [self setUI];
    [self getAccountList];

}

- (void)getAccountList {
    NSString *url = @"/tiny-shop/v1/bank-account/index";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @(self.page);
    [[NetWorksTools shared] requestWithMethod:GET url:url params:params successBlock:^(NSDictionary * _Nonnull response) {
        [self endRefresh];
        self.accountList = response[@"data"];

        if (self.accountList.count > 0) {
            self.accountDic = self.accountList[0];
        }
        [self.tableView reloadData];
    } failureBlock:^(NSError * _Nonnull error) {

    }];
}

- (void) setUI {
    self.view.backgroundColor = ColorF5;

    UIButton *withdrawBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [withdrawBtn setTitle:@"提现" forState:UIControlStateNormal];
    [withdrawBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    withdrawBtn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
    withdrawBtn.cornerRadius = 5;
    withdrawBtn.backgroundColor = ThemeColor;
    [self.view addSubview:withdrawBtn];
    [withdrawBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-10-kBottomWhiteHeight);
        make.height.mas_equalTo(45);
        make.leading.equalTo(self.view).offset(15);
    }];

    WeakSelf;
    withdrawBtn.block = ^(UIButton *sender) {
        [weakSelf withdrawAction];
    };

    self.tableView.backgroundColor = ColorF5;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(withdrawBtn.mas_top).offset(-10);
    }];

    [self.tableView registerClass:[WithdrawSecondTopCell class] forCellReuseIdentifier:@"WithdrawSecondTopCell"];
    [self.tableView registerClass:[WithdrawSecondMoneyCell class] forCellReuseIdentifier:@"WithdrawSecondMoneyCell"];
    [self.tableView registerClass:[WithdrawSecondSendMsgCell class] forCellReuseIdentifier:@"WithdrawSecondSendMsgCell"];

}

- (void) withdrawAction {

    if (self.moneyTf.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入提现金额"];
        return;
    }

    if (self.codeTf.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的验证码"];
        return;
    }

    NSString *url = @"/tiny-shop/v1/commission-withdraw/apply";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"bank_account_id"] = self.accountDic[@"id"];
    params[@"money"] = self.moneyTf.text;
    params[@"code"] = self.codeTf.text;
    [[NetWorksTools shared] requestWithMethod:POST url:url params:params successBlock:^(NSDictionary * _Nonnull response) {

        } failureBlock:^(NSError * _Nonnull error) {

        }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        WithdrawSecondTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawSecondTopCell"];
        cell.dic = self.accountDic;
        return cell;
    }

    if (indexPath.section == 1) {
        WithdrawSecondMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawSecondMoneyCell"];
        NSDictionary *account = self.userDic[@"account"];
        self.moneyTf = cell.moneyTf;
        cell.dic = account;
        return cell;
    }

    WithdrawSecondSendMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawSecondSendMsgCell"];
    cell.dic = self.accountDic;
    self.codeTf = cell.codeTf;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) { //选择账户
        WithdrawAccountVC *vc = [[WithdrawAccountVC alloc] init];
        vc.accountList = self.accountList;
        WeakSelf;
        vc.chooseAccountBlock = ^(NSDictionary * _Nonnull dic) {
            weakSelf.accountDic = dic;
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ColorF5;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ColorF5;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}


@end
